package com.vcredit.creditcat.security;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.vcredit.creditcat.model.User;
/**
 * 取得当前用户权限
 */
public class SecurityHelper {
	/**
	 * 取得当前用户信息UserDetails
	 * @return
	 */
    private static UserDetails getUserDetails() {
    	try {
    		Object userDetails = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			
			if (userDetails instanceof UserDetails) {
				return (UserDetails)userDetails;
			}
    	} catch (Exception e) {}
    	
    	return null; 
	}	
    
    public static String getUserName() {
    	UserDetails userDetail = getUserDetails();
    	return userDetail==null?"":userDetail.getUsername();
	}	
    
    public static User getUser() {
    	UserDetails userDetail = getUserDetails();
    	return userDetail==null?null:(User)userDetail;
	}	
    
    public static Long getUserId() {
    	User user = getUser();
    	return user==null?null:user.getUserId();
	}	

}
